package com.lixinkeji.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.drake.brv.PageRefreshLayout
import com.drake.brv.utils.page
import com.lixinkeji.base.databinding.LayoutCommonListBinding

/**
 *
 * @ClassName BaseListFragment
 * @Author lwc
 * @Email 1679114787@qq.com
 * @Date 2022/4/13 11:44
 * @Des
 */
abstract class BaseListFragment : Fragment() {

    lateinit var recyclerView: RecyclerView
    lateinit var refreshLayout: PageRefreshLayout
    open fun hasRefresh(): Boolean = true
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val mainBinding = LayoutCommonListBinding.inflate(inflater)
        recyclerView = RecyclerView(requireContext())
        mainBinding.root.addView(recyclerView)
        recyclerView.layoutParams = RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
        )
        if (hasRefresh()) {
            refreshLayout = recyclerView.page()
            refreshLayout.layoutParams = RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT
            )
            refreshLayout.onRefresh {
                getData()
            }
        }
        return mainBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        iniView()
    }

    abstract fun iniView()

    open fun PageRefreshLayout.getData() {}
}



